import 'package:json_annotation/json_annotation.dart';

part 'task_process_step_model.g.dart';

@JsonSerializable()
class TaskProcessStepModel {
  @JsonKey(name: 'showSubmitResult', defaultValue: false)
  final bool showSubmitResult;

  @JsonKey(name: 'isRefuse', defaultValue: false)
  final bool isRefuse;

  @JsonKey(name: 'stepComment', defaultValue: '')
  final String stepComment;

  @JsonKey(name: 'stepTitle', defaultValue: '')
  final String stepTitle;

  @JsonKey(name: 'displayComplete', defaultValue: false)
  final bool displayComplete;

  @JsonKey(name: 'completeRateValue', defaultValue: 0.0)
  final double completeRateValue;

  TaskProcessStepModel({
    required this.showSubmitResult,
    required this.isRefuse,
    required this.stepComment,
    required this.stepTitle,
    required this.displayComplete,
    required this.completeRateValue,
  });

  factory TaskProcessStepModel.fromJson(Map<String, dynamic> json) =>
      _$TaskProcessStepModelFromJson(json);

  Map<String, dynamic> toJson() => _$TaskProcessStepModelToJson(this);

  // 根据 iOS 中的逻辑，添加判断是否为拒绝状态的方法
  bool get isRefusedStep {
    return stepTitle.contains('未通过') || stepTitle.contains('拒绝');
  }

  @override
  String toString() {
    return 'TaskProcessStepModel{showSubmitResult: $showSubmitResult, isRefuse: $isRefuse, stepComment: $stepComment, stepTitle: $stepTitle, displayComplete: $displayComplete, completeRateValue: $completeRateValue}';
  }

  // 复制方法，便于创建修改后的实例
  TaskProcessStepModel copyWith({
    bool? showSubmitResult,
    bool? isRefuse,
    String? stepComment,
    String? stepTitle,
    bool? displayComplete,
    double? completeRateValue,
  }) {
    return TaskProcessStepModel(
      showSubmitResult: showSubmitResult ?? this.showSubmitResult,
      isRefuse: isRefuse ?? this.isRefuse,
      stepComment: stepComment ?? this.stepComment,
      stepTitle: stepTitle ?? this.stepTitle,
      displayComplete: displayComplete ?? this.displayComplete,
      completeRateValue: completeRateValue ?? this.completeRateValue,
    );
  }
}

extension TaskProcessStepExtensions on TaskProcessStepModel {
  /// 根据 iOS 中的逻辑，在解析后处理 isRefuse 字段
  TaskProcessStepModel withRefuseDetection() {
    final isRefused = stepTitle.contains('未通过') || stepTitle.contains('拒绝');
    return copyWith(isRefuse: isRefused);
  }
}

/// 用于处理列表的扩展方法
extension TaskProcessStepListExtensions on List<TaskProcessStepModel> {
  /// 批量处理 isRefuse 字段检测
  List<TaskProcessStepModel> withRefuseDetection() {
    return map((step) => step.withRefuseDetection()).toList();
  }
}